import 'package:flutter/cupertino.dart';
import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_team/deposit_record_team_logic.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:get/get.dart';
import 'package:flutter_09_game/base/controller/base_list_controller.dart';
import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_team.dart';
import 'package:flutter_09_game/pages/deposit/utils_deposit.dart';
import 'package:flutter_09_game/pages/main/keep_alive_widget.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';

// import 'package:get/get.dart';
class DepositRecordTeamListLogic extends BaseListMoreController {
  DepositRecordTeamListLogic({
    required this.type,
  });

  // final bool onlyBet;
  final String type;

  RxString timeRx = LocaleKeys.today.tr.obs;

  List<String> gameItems = [LocaleKeys.frogBattleRoyale.tr, 'Slots'];

  // 0 大逃杀  1三方游戏
  RxInt gameType = 0.obs;

  // RxString symbolRx = (AppUserManager().currentWalletRx.value.symbol ?? '').obs;

  TextEditingController idController = TextEditingController();

  // 日期范围相关属性
  Rx<DatePeriod?> selectedDateRange = Rx<DatePeriod?>(null);
  Rx<ModelRecordTeam> recordTeamRx = Rx(ModelRecordTeam());

  @override
  Future<List> loadData() async {
    final dateRange = UtilsDeposit.calculateDateRange(timeRx.value,
        selectedDateRange: selectedDateRange.value);

    var params = {
      'only_bet': getIsHash(),
      'page': pageNum,
      'size': pageSize,
      'symbol': AppUserManager().currentWalletRx.value.symbol,
      'start': dateRange['start'],
      'end': dateRange['end'],
      'user_identity': idController.text.trim()
    };

    getUserTeamStatistics(params);

    HttpResponse response = await HttpManager().get(
        getTag() ==
                UtilsOther.recordTeamOptions[2]
            ? (gameType.value == 0
                ? API.getGameBattleTeamMemberStatistics
                : API.getGameTeamMemberStatistics)
            : API.getUserTeamMemberStatistics,
        params: params,
        fromJson: ModelRecordTeamItem.fromJson);

    // ?only_bet=false&start=2025-09-15+00:00:00&end=2025-09-15+23:59:59&symbol=TRX&user_identity=&size=10&page=1
    return response.data ?? [];
    // ModelRecordTeamItem
  }

  getUserTeamStatistics(params) async {

    HttpResponse response = await HttpManager().get(
      getTag()  == UtilsOther.recordTeamOptions[2]
          ? (gameType.value == 0
              ? API.getGameBattleTeamStatistics
              : API.getGameTeamStatistics)
          : API.getUserTeamStatistics,
      fromJson: ModelRecordTeam.fromJson,
      params: params,
    );
    if (response.isSuccess) {
      recordTeamRx.value = response.data;
    }
  }

  /// 更新时间选择
  void updateTimeSelection(String timeType) {
    if (timeType == LocaleKeys.custom.tr) {
      UtilsDeposit.showDateRangePicker(selectedDateRange.value).then((value) {
        if (value is DatePeriod) {
          selectedDateRange.value = value;
          refreshController.requestRefresh();
          timeRx.value = timeType;
        }
      });
    } else {
      timeRx.value = timeType;
      // 清除自定义日期范围
      selectedDateRange.value = null;
      refreshController.requestRefresh();
    }
  }

  onToggleExpand(ModelRecordTeamItem item) {
    item.isExpanded = item.isExpanded == true ? false : true;
    update();
  }

  bool getIsHash() {
    return getTag() !=
        UtilsOther.recordTeamOptions.first;
  }

 String getTag() {
    return type.replaceAll('1', '').replaceAll('2', '');
  }

}
